package com.wpp;

import org.junit.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;

/*
 * TCP测试：建立客户端和服务端
 *
 *
 *
 *
 * */
public class TCPTest {

    //1.建立客户端
    @Test
    public void client() {



        Socket socket = null;
        OutputStream os = null;
        try {
            InetAddress byName = InetAddress.getByName("127.0.0.1");//获取ip地址
            socket = new Socket(byName, 9999);//得知对方的端口号
            os = socket.getOutputStream();//数据输出
            os.write("我是客户端".getBytes());//char型数据转换为byte字节发送
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }

    //2.建立服务器端
    @Test
    public void Server() {
        ServerSocket serverSocket = null;//
        Socket accept = null;
        InputStream inputStream = null;
        ByteArrayOutputStream bos = null;

        try {

            serverSocket = new ServerSocket(9999); //创建一个ServerSocket，并设置端口号
            accept = serverSocket.accept();//允许连接
            inputStream = accept.getInputStream();//获取输入流

            bos = new ByteArrayOutputStream();//把获取的字节数据全部存入，并且最后一次性调用toString方法
            byte[] bytes = new byte[5];
            int len;
            while ((len = inputStream.read(bytes)) != -1) {
                bos.write(bytes, 0, len);
            }
            System.out.println(bos.toString());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭流
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (accept != null) {
                try {
                    accept.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (serverSocket != null) {
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }

    /**
    *用于测试:
    *
    */
    @Test
    public void test(){


        //存的是内存地址
        String str = "abc";

        String s1="a";
        String s2="bc";


        String ss = s1+s2;

        System.out.println(str==ss);

    }
}
